package com.ioftv.controller;

import com.ioftv.common.Result;
import com.ioftv.entity.SysMenu;
import com.ioftv.entity.SysUser;
import com.ioftv.service.SysMenuService;
import com.ioftv.service.SysUserService;
import com.ioftv.service.UserMenuService;
import com.ioftv.util.UserContextUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 菜单权限管理控制器
 * 实现菜单管理、用户权限分配等功能
 */
@RestController
@RequestMapping("/admin")
public class MenuPermissionController {

    @Autowired
    private SysMenuService sysMenuService;

    @Autowired
    private SysUserService sysUserService;

    @Autowired
    private UserMenuService userMenuService;

    /**
     * 查询所有菜单列表
     * GET /admin/menus
     */
    @GetMapping("/menus")
    public Result<List<SysMenu>> getAllMenus(HttpServletRequest request) {
        // 获取当前用户信息
        Long userId = UserContextUtil.getCurrentUserId(request);
        String username = UserContextUtil.getCurrentUsername(request);
        String role = UserContextUtil.getCurrentUserRole(request);

        List<SysMenu> menus = sysMenuService.getAllMenus();
        return Result.success(menus);
    }

    /**
     * 根据用户ID查询用户拥有的菜单权限
     * GET /admin/menus/user/{userId}
     */
    @GetMapping("/menus/user/{userId}")
    public Result<List<Long>> getUserMenuIds(@PathVariable Long userId, HttpServletRequest request) {
        // 获取当前用户信息
        Long currentUserId = UserContextUtil.getCurrentUserId(request);
        String currentUsername = UserContextUtil.getCurrentUsername(request);
        String currentRole = UserContextUtil.getCurrentUserRole(request);

        // 检查用户是否存在
        SysUser user = sysUserService.getById(userId);
        if (user == null) {
            return Result.error("用户不存在");
        }

        List<Long> menuIds = userMenuService.getMenuIdsByUserId(userId);
        return Result.success(menuIds);
    }

    /**
     * 为用户分配菜单权限
     * POST /admin/user-menu
     */
    @PostMapping("/user-menu")
    public Result<Void> assignMenusToUser(@RequestBody AssignMenuRequest assignMenuRequest, HttpServletRequest request) {
        // 获取当前用户信息
        Long currentUserId = UserContextUtil.getCurrentUserId(request);
        String currentUsername = UserContextUtil.getCurrentUsername(request);
        String currentRole = UserContextUtil.getCurrentUserRole(request);

        // 参数校验
        if (assignMenuRequest.getUserId() == null) {
            return Result.error("用户ID不能为空");
        }

        if (assignMenuRequest.getMenuIds() == null) {
            return Result.error("菜单ID列表不能为空");
        }

        // 检查用户是否存在
        SysUser user = sysUserService.getById(assignMenuRequest.getUserId());
        if (user == null) {
            return Result.error("用户不存在");
        }

        // 为用户分配菜单权限
        userMenuService.assignMenusToUser(assignMenuRequest.getUserId(), assignMenuRequest.getMenuIds());
        return Result.success(null);
    }

    /**
     * 查询所有用户列表（用于权限分配）
     * GET /admin/users
     */
    @GetMapping("/users")
    public Result<List<SysUser>> getAllUsers(HttpServletRequest request) {
        // 获取当前用户信息
        Long currentUserId = UserContextUtil.getCurrentUserId(request);
        String currentUsername = UserContextUtil.getCurrentUsername(request);
        String currentRole = UserContextUtil.getCurrentUserRole(request);

        List<SysUser> users = sysUserService.list();
        return Result.success(users);
    }

    /**
     * 分配菜单请求DTO
     */
    public static class AssignMenuRequest {
        private Long userId;
        private List<Long> menuIds;

        public Long getUserId() {
            return userId;
        }

        public void setUserId(Long userId) {
            this.userId = userId;
        }

        public List<Long> getMenuIds() {
            return menuIds;
        }

        public void setMenuIds(List<Long> menuIds) {
            this.menuIds = menuIds;
        }
    }
}